package com.cyzy.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cyzy.utils.JwtUtil;
import com.cyzy.utils.RedisUtil;
import com.cyzy.utils.SpringContextUtil;
import jakarta.servlet.http.HttpSession;
import jakarta.websocket.HandshakeResponse;
import jakarta.websocket.server.HandshakeRequest;
import jakarta.websocket.server.ServerEndpointConfig;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;

import java.util.List;
import java.util.Map;

/**
 * @author cuncun
 * @create 2025-05-29 下午8:02
 */
public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator {

    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response){
        RedisUtil redisUtil = SpringContextUtil.getBean(RedisUtil.class);
        Map<String, List<String>> headers = request.getHeaders();

        System.out.println(headers + " -----headers");
        //从 "Cookie" 请求头中提取用户在redis中的key值  token=123456
        List<String> cookieHeaders = headers.get("Cookie");
        //从receiver 请求头中提取发送方的类型
        List<String> senderTypeList = headers.get("sender_type");
        String senderType = senderTypeList.get(0);
        //从receiver 请求头中提取历史记录id
        List<String> conversationIdList = headers.get("conversationId");
        String conversationId = conversationIdList.get(0);

        System.out.println(senderType + " ----------senderType");
        System.out.println(conversationId + " ----------历史记录id");
        System.out.println(cookieHeaders + " ----cookieHeaders");
        String jsessionId = null;
        if (cookieHeaders != null) {
            for (String cookie : cookieHeaders) {
                if (cookie.contains("token")) {
                    String[] parts = cookie.split("=");
                    if (parts.length > 1) {
                        jsessionId = parts[1].split(";")[0].trim(); // 提取 token 的值
                        break;
                    }
                }
            }
        }

        if (jsessionId == null) {
            System.out.println("未找到 token");
            return;
        }

        System.out.println(jsessionId + "-----jsessionId");
        // 使用 JSESSIONID 从 Redis中获取用户信息
        String userInRedis = redisUtil.get(jsessionId);
        System.out.println(userInRedis + " ------userInRedis");
        Map<String, Object> user = JwtUtil.parseToken(userInRedis);

        System.out.println(user + "     user");

        if (user == null) {
            System.out.println("Redis 中未找到该用户的登录信息");
            return;
        }
        
        // 将用户信息存入 EndpointConfig
        sec.getUserProperties().put("userId",String.valueOf(user.get("id")));
        sec.getUserProperties().put("username", user.get("acc"));
        sec.getUserProperties().put("conversationId",conversationId);
        sec.getUserProperties().put("senderType",senderType);
        System.out.println(sec.getUserProperties() + "-------sec");
    }


}
